package com.ynsj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynsj.domain.Notice;
import com.ynsj.domain.WebConfig;
import com.ynsj.model.R;
import com.ynsj.model.WebLog;
import com.ynsj.service.WebConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

@RestController
@Api(tags = "webConfig的控制器")
@RequestMapping("/webConfigs")
public class WebConfigController {

    @Autowired
    private WebConfigService webConfigService;

    @GetMapping
    @ApiOperation("分页查询webConfig")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "页大小"),
            @ApiImplicitParam(name = "name",value = "资源名称"),
            @ApiImplicitParam(name = "type",value = "资源类型")
    })
    @PreAuthorize("hasAuthority('web_config_query')")
    public R<Page<WebConfig>> findNoticePage(@ApiIgnore Page<WebConfig> page, String name, String type){
        return R.ok(webConfigService.findByPage(page,name,type));
    }

    @PostMapping
    @ApiOperation("新增一个资源配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig",value = "webConfig的json")
    })
    @PreAuthorize("hasAuthority('web_config_create')")
    public R addWebConfig(@RequestBody @Validated WebConfig webConfig){
        boolean isOk = webConfigService.save(webConfig);
        if (isOk){
            return R.ok("新增资源配置成功");
        }
        return R.fail("新增资源配置失败");
    }

    @PatchMapping
    @ApiOperation("修改一个资源配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webConfig",value = "webConfig的json")
    })
    @PreAuthorize("hasAuthority('web_config_update')")
    public R updateWebConfig(@RequestBody @Validated WebConfig webConfig){
        boolean isOk = webConfigService.updateById(webConfig);
        if (isOk){
            return R.ok("更新资源配置成功");
        }
        return R.fail("更新资源配置失败");
    }

    @PostMapping("/delete")
    @ApiOperation("删除资源配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "删除的资源id")
    })
    @PreAuthorize("hasAuthority('web_config_delete')")
    public R delWebConfig(@RequestBody String[] ids){
        boolean isOk = webConfigService.removeByIds(Arrays.asList(ids));
        if (isOk){
            return R.ok("删除资源成功");
        }
        return R.fail("删除资源失败");
    }
}
